package nl.myhealth.app.myhealthapp;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONObject;

import nl.myhealth.app.myhealthapp.API.RequestTask;
import nl.myhealth.app.myhealthapp.Adapter.AdapterFiller;
import nl.myhealth.app.myhealthapp.Adapter.MeasurementAdapter;
import nl.myhealth.app.myhealthapp.Measurement.Measurement;
import nl.myhealth.app.myhealthapp.Measurement.PulseMeasurement;


public class PulseActivity extends ActionBarActivity implements AdapterView.OnItemClickListener,AdapterFiller{

    private ListView listView;
    private Button measurementButton;
    private MeasurementAdapter<Measurement> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pulse);

        listView = (ListView)findViewById(R.id.activity_pulse_list);
        adapter = new MeasurementAdapter(this);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);
    }

    public void startPulseMeting(View view)
    {
        if(MyHealthApplication.socket != null)
        {
            if(MyHealthApplication.socket.isConnected())
            {
                
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        new RequestTask(this).execute(MyHealthApplication.serviceURI + "/pulses");
    }

    public void fillListWithJsonMeasurements(String json)
    {
        try {
            adapter.clear();
            JSONArray jsonArray = new JSONArray(json);
            for(int i = 0; i < jsonArray.length(); i++) {
                JSONObject row = jsonArray.getJSONObject(i);
                adapter.add(new PulseMeasurement(row.toString()));
            }
            adapter.notifyDataSetChanged();
        }catch (Exception e)
        {
            e.printStackTrace();
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.main_menu_dashboard) {
            Intent dashBoardIntent = new Intent(this, DashboardActivity.class);
            startActivity(dashBoardIntent);
            return true;
        }
        if (id == R.id.main_menu_profile) {
            Intent profileIntent = new Intent(this,ProfileActivity.class);
            startActivity(profileIntent);
            return true;
        }
        if (id == R.id.main_menu_bills) {
            Intent billsIntent = new Intent(this, BillsActivity.class);
            startActivity(billsIntent);
            return true;
        }
        if (id == R.id.main_menu_pulse) {
            Intent pulseIntent = new Intent(this,PulseActivity.class);
            startActivity(pulseIntent);
            return true;
        }
        if (id == R.id.main_menu_blood_pressure) {
            Intent pressureIntent = new Intent(this,PressureActivity.class);
            startActivity(pressureIntent);
            return true;
        }
        if (id == R.id.main_menu_ecg) {
            Intent ecgIntent = new Intent(this,ECGActivity.class);
            startActivity(ecgIntent);
            return true;
        }
        if (id == R.id.main_menu_urine) {
            Intent urineIntent = new Intent(this,UrineActivity.class);
            startActivity(urineIntent);
            return true;
        }
        if (id == R.id.main_menu_device) {
            Intent deviceIntent = new Intent(this, DeviceActivity.class);
            startActivity(deviceIntent);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        String json = adapter.getItem(i).toJsonString();
        new PulseMeasurement(json);
        Toast.makeText(this,json,Toast.LENGTH_SHORT).show();
    }
}
